import {createRouter, createWebHistory} from 'vue-router'
import type {RouteRecordRaw} from 'vue-router'
import {getCurrUserMenu} from "@/api/sys/webMenu";
import storage from "@/config/storage";

declare module 'vue-router' {
    interface RouteMeta {
        fullScreen: boolean
        showName?: string
    }
}

const loginRoutes: RouteRecordRaw[] = [
    {
        path: '/',
        name: 'root',
        redirect: "/login",
        meta: {
            fullScreen: true,
        }
    },
    {
        path: '/login',
        name: 'login',
        component: () => import("@/views/system/login/Login.vue"),
        meta: {
            fullScreen: true,
        }
    }
]

const otherRoutes: RouteRecordRaw[] = [
    {
        path: '/markdown/preview',
        name: 'SeeMarkdown',
        component: () => import("@/views/common/rich-text/MarkdownPreview.vue"),
        meta: {
            fullScreen: true,
        }
    },
]

const systemRoutes: RouteRecordRaw[] = [
    {
        name: 'UserMgr',
        path: '/system/user/mgr',
        component: () => import("@/views/system/user/SysUserMgr.vue"),
        meta: {
            pageTitle: '用户管理',
            fullScreen: false,
        }
    },
    {
        name: 'MenuMgr',
        path: '/system/web/menu/mgr',
        component: () => import("@/views/system/web/menu/SysWebMenuMgr.vue"),
        meta: {
            pageTitle: '菜单管理',
            fullScreen: false,
        }
    },
    {
        name: 'RoleMgr',
        path: '/system/role/mgr',
        component: () => import("@/views/system/role/SysRoleMgr.vue"),
        meta: {
            pageTitle: '角色管理',
            fullScreen: false,
        }
    },
    {
        name: 'PermissionMgr',
        path: '/system/permission/mgr',
        component: () => import("@/views/system/permission/SysPermissionMgr.vue"),
        meta: {
            pageTitle: '权限管理',
            fullScreen: false,
        }
    },
    {
        name: 'DictMgr',
        path: '/system/dict/mgr',
        component: () => import("@/views/system/dict/SysDictMgr.vue"),
        meta: {
            pageTitle: '字典管理',
            fullScreen: false,
        }
    },
    {
        name: 'DictDetails',
        path: '/system/dict/details',
        component: () => import("@/views/system/dict/SysDictDetailsMgr.vue"),
        meta: {
            pageTitle: '字典详情',
            fullScreen: false,
        }
    },
    {
        name: 'ConfigMgr',
        path: '/system/config/mgr',
        component: () => import("@/views/system/config/SysConfigMgr.vue"),
        meta: {
            pageTitle: '系统配置',
            fullScreen: false,
        }
    },
    {
        name: 'MyInfo',
        path: '/system/user/myInfo',
        component: () => import("@/views/system/user/MyInfo.vue"),
        meta: {
            pageTitle: '个人信息',
            fullScreen: false,
        }
    },
]

function getRoutes(): RouteRecordRaw[] {
    let routes: RouteRecordRaw[] = [];
    routes.push(...loginRoutes)
    routes.push(...otherRoutes)
    routes.push(...systemRoutes)
    return routes;
}

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: getRoutes()
})

export default router

export function loadDynamicRoutes() {
    return new Promise((resolve, reject) => {
        getCurrUserMenu().then(res => {
            let {menus} = res.data
            storage.setItem("menus", JSON.stringify(menus))
            resolve(0)
        })
    })
}

